﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ECCIT;
using ECCIT.Extension.Weibo_v2.Common;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.DALFactory;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.Mobile.Model.Events;
using OgilvyOne.UKNow.Mobile.Model.MyCalendar;
using OgilvyOne.UKNow.Mobile.Utilities;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Utility;

namespace OgilvyOne.UKNow.Mobile.APIs.Events
{
  public class UserEvents:IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }
        public void ProcessRequest(HttpContext context)
        {
            int pagesize = 10;
            int currindex =1;
            var strpagesize = context.Request["page_size"];
            var current_page = context.Request["current_page"];
            if (strpagesize!=null)
                int.TryParse(strpagesize, out pagesize);
            if (current_page!=null)
                int.TryParse(current_page, out currindex);
            IDALEvents events = EventsDALFactory.Create(2052);
            PagingParameter pagpar = PagingParameterFactory.CreateInstance(true, currindex, pagesize, null);
            int iTotalPage;
            int iTotalRow;
            IList<UserEventList> list= events.GetUserEventlist(pagpar, out iTotalPage, out iTotalRow);
            List<UserlistEvents> userEventses = new List<UserlistEvents>();
            foreach (var userEventList in list)
            {
                UserlistEvents mEvents = new UserlistEvents();
                mEvents.Commentimg = "";
                mEvents.Content = userEventList.Content;
                mEvents.Eventid = userEventList.Eventid;
                mEvents.Title = userEventList.Title;
                mEvents.Showtime = DateTimeFormatter.FormatDateTime(userEventList.Showtime);
                mEvents.UserID = userEventList.UserID;
                mEvents.UserName = userEventList.UserName;
                if(!string.IsNullOrEmpty(userEventList.Commentimg))
                {
                    mEvents.Commentimg = ConfigCacheManager.DomainName +
                                                       ConfigCacheManager.FilePath.GetFrontImageVirtualPath(
                                                           userEventList.Commentimg);
                   
                }
                mEvents.avatar = "";
                if(!string.IsNullOrEmpty(userEventList.avatar))
                {
                    mEvents.avatar = ConfigCacheManager.DomainName +
                                                      ConfigCacheManager.FilePath.GetFrontImageVirtualPath(
                                                          userEventList.avatar);
                }
                userEventses.Add(mEvents);

            }

            var hotlist = new PageOfItems<UserlistEvents>();
            hotlist.PageIndex = currindex;
            hotlist.PageSize = pagesize;
            hotlist.Data = userEventses;// list.Cast<UserEventList>().ToList();
            hotlist.TotalItemCount = iTotalRow; 
            hotlist.TotalPageCount = iTotalPage; 
            string s = JsonSerializationHelper.ObjectToJson(hotlist);
            context.Response.Write(ResultUtility.ReturnResultJson("0", s, "获取用户的最新评论、收藏、签到信息"));

        }
    }
}
